package org.xqyy.eval.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.xqyy.eval.model.*;
import org.xqyy.eval.service.IEvalMasterService;
import org.xqyy.eval.service.IEvalService;
import org.xqyy.eval.service.IQuestionService;
import org.xqyy.eval.service.impl.EvalMasterService;
import org.xqyy.eval.util.DataFormatUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luoxiaoming on 17-2-21.
 */

@Controller
@RequestMapping("/eval")
public class EvalController {

    @Autowired
    private IEvalService evalService;
    @Autowired
    private IQuestionService questionService;

    @Autowired
    private IEvalMasterService evalMasterService;


    @RequestMapping("/loadEvalMasterData")
    public
    @ResponseBody
    Map<String, Object> loadEvalMasterData(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap();
        String month = request.getParameter("month");
        if (!StringUtils.hasLength(month)) {
            month = DataFormatUtil.defectDateFormat.format(new Date());
        }
        res.put("month", month);
        Map<String, Object> user = (Map<String, Object>) request.getSession().getAttribute("user"); //获取当前会话的session中的user
        if (user.get("type").equals("B")) {
            res.put("deptCode", user.get("deptCode"));
        }
        List<Map<String, Object>> data = evalService.loadEvalMasterData(res);
        res.put("root", data);
        return res;
    }


    @RequestMapping("/generatorNextMonthData")
    public
    @ResponseBody
    Map<String, Object> generatorNextMonthData(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            Map<String, Object> user = (Map<String, Object>) request.getSession().getAttribute("user");
            res.put("username", user.get("username"));
            if (!user.get("type").equals("A")){
                throw new RuntimeException("权限不足!");
            }
            String beginTime = request.getParameter("beginTime");
            String endTime = request.getParameter("endTime");
            res.put("beginTime", beginTime);
            res.put("endTime", endTime);
            evalService.generatorNextMonthData(res);

            res.put("success", "操作成功!");
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/loadEvalPageTabs")
    public
    @ResponseBody
    Map<String, Object> loadEvalPageTabs(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            Map<String, Object> user = (Map<String, Object>) request.getSession().getAttribute("user");
            List<Map<String, Object>>  list = questionService.getpaperId((String) user.get("deptCode"));
            if (list.size() >0 ){
                Map<String, Object> objectMap = list.get(0);
                Integer paperId = (Integer)objectMap.get("paperId");
                res.put("paperId", paperId);
            }
            res.put("username", user.get("username"));
            res.put("deptCode", user.get("deptCode"));
            List<Map<String, Object>> data = this.evalService.loadEvalPageTabs(res);
            res.put("data", data);
            res.put("success", true);
        } catch (Exception e) {
            res.put("failure", false);
            res.put("result", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/loadEvalPageContent")
    @ResponseBody
    public Map<String, Object> loadEvalPageContent(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> res = new HashMap<>();
        Map<String, Object> user = (Map<String, Object>) request.getSession().getAttribute("user");
        String groupCode = request.getParameter("groupCode"); //Fun_hanxn_20190321_mark
        String paperId = request.getParameter("paperId");  //Fun_hanxn_20190321_mod
        String month = request.getParameter("month");
        if (!StringUtils.hasLength(month)) {
            params.put("month", DataFormatUtil.defectDateFormat.format(new Date()));
        } else {
            params.put("month", month);
        }
        //params.put("groupCode", groupCode);  //Fun_hanxn_20190321_mark
        /*Fun_hanxn1920326_mark_str*/
        /*List<Map<String, Object>>  list = questionService.getpaperId((String) user.get("deptCode"));
        if (list.size() > 0 ){
            Map<String, Object> objectMap = list.get(0);
            params.put("paperId", ((Integer)objectMap.get("paperId")).toString());
        }*/
        /*Fun_hanxn1920326_mark_end*/
        /*Fun_hanxn1920326_add_str*/
        params.put("paperId", paperId);
        /*Fun_hanxn1920326_add_end*/
    /*    List<Map<String, Object>>  list_group_code = questionService.getgroupCode((String) user.get("deptCode"));
        if (list_group_code.size() > 0 ){
            Map<String, Object> objectMap = list_group_code.get(0);
            params.put("groupCode", (String)objectMap.get("group_code"));
        }*/
        String deptCode = request.getParameter("deptCode");
        if (StringUtils.hasLength(deptCode)) {
            params.put("deptCode", deptCode);
        } else {
            //Map<String, Object> user = (Map<String, Object>) request.getSession().getAttribute("user");
            params.put("deptCode", user.get("deptCode"));
        }
        Map<String, Object> data = this.evalService.loadEvalPageContent(params);
        res.put("data", data);
        return res;
    }

    @RequestMapping("/updateEval")
    @ResponseBody
    public Map<String, Object> updateEval(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> res = new HashMap<>();


        try {
            String masterId = request.getParameter("masterId");
            String questionId = request.getParameter("questionId");
            String grade = request.getParameter("grade");
            String groupCode = request.getParameter("groupCode");
            params.put("masterId", masterId);
            params.put("groupCode", groupCode);
            params.put("questionId", questionId);
            params.put("grade", grade);
            String mark = request.getParameter("mark");
            if (StringUtils.hasLength(mark)) {
                params.put("mark", mark);
            }
            if (!StringUtils.hasLength(questionId) && !StringUtils.hasLength(mark)) {
                params.put("mark", " ");
            }
            String flag= this.evalService.modifyDetail(params);
            if(flag.equals("1"))
            res.put("success", true);
            else  res.put("failure", flag);
        } catch (Exception e) {
            res.put("failure", false);
            res.put("result", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/submitEval")
    @ResponseBody
    public Map<String, Object> submitEval(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> res = new HashMap<>();
        Map<String, Object> user = (Map<String, Object>) request.getSession().getAttribute("user");
        try {
            String paperId = request.getParameter("paperId"); //Fun_hanxn190321_mod
            //String groupCode = request.getParameter("groupCode");
            String month = request.getParameter("month");

            if (!StringUtils.hasLength(month)) {
                params.put("month", DataFormatUtil.defectDateFormat.format(new Date()));
            } else {
                params.put("month", month);
            }
            //params.put("groupCode", groupCode);
            String deptCode = request.getParameter("deptCode");
            if (StringUtils.hasLength(deptCode)) {
                params.put("deptCode", deptCode);
            } else {
                //Map<String, Object> user = (Map<String, Object>) request.getSession().getAttribute("user");
                params.put("deptCode", user.get("deptCode"));
            }
            List<Map<String, Object>>  list_group_code = questionService.getgroupCode((String) user.get("deptCode"));
            if (list_group_code.size() > 0 ){
                Map<String, Object> objectMap = list_group_code.get(0);
                params.put("groupCode", (String)objectMap.get("group_code"));
            }
            params.put("status", "1");
            params.put("empty", "1");
            params.put("paperId", paperId); //Fun_hanxn190321_mod

          String flag=  this.evalService.submitEval(params);
            if(flag.equals("1"))
            res.put("success", "提交成功");
            else   res.put("failure", flag);
        } catch (Exception e) {
            res.put("failure", "提交失败");
            res.put("result", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }


    @RequestMapping("/evalPage/{paperId}/{cycledate}")
    public ModelAndView evalPage(HttpServletRequest request, @PathVariable("paperId") Integer paperId,
                                 @PathVariable("cycledate") String cycledate){
        HttpSession session = request.getSession();
        TUser user = (TUser)  session.getAttribute("user");
        List<TQuestion> questionList = questionService.selectQuestionByPaperId(paperId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("jsp/eval/evalPage");
        modelAndView.addObject("deptid",user.getDeptId());
        modelAndView.addObject("paperId",paperId);
        modelAndView.addObject("questionList",questionList);
        modelAndView.addObject("cycledate",cycledate);
        return modelAndView;
    }

    /**
     * 查看已经填报的调查问卷情况
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/viewevalPage/{id}")
    public ModelAndView viewevalPage(HttpServletRequest request, @PathVariable("id") String id){
        HttpSession session = request.getSession();
        TUser user = (TUser)  session.getAttribute("user");
        TEvalMaster tEvalMaster =  evalMasterService.findTEvalMasterById(id);
        List<HashMap<String,Object>> hashMapList =  evalService.evalEdList(tEvalMaster.getEvalyearmonth()
                ,user.getDeptId(),tEvalMaster.getPaperid());
        List<TQuestion> tQuestionList =  questionService.selectQuestionByPaperId(tEvalMaster.getPaperid());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("jsp/eval/viewevalPage");
        modelAndView.addObject("deptid",user.getDeptId());
        modelAndView.addObject("paperId",tEvalMaster.getPaperid());
        modelAndView.addObject("cycledate",tEvalMaster.getEvalyearmonth());
        modelAndView.addObject("tQuestionList",tQuestionList);
        modelAndView.addObject("hashMapList",hashMapList);
        return modelAndView;
    }
}
